package com.lesso.lpq.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lpq.sunflower.core.ApiResponse;
import com.lesso.lpq.sunflower.pojo.dto.AttendanceRuleDTO;
import com.lesso.lpq.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import com.lesso.lpq.sunflower.pojo.vo.AttendanceRuleVO;
import com.lesso.lpq.sunflower.service.IAttendanceRuleService;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName : DeviceController
 * @Description :
 * @Author : 罗培棋
 * @Date: 2025-07-05 08:48
 */
@RestController
@RequestMapping("/base/attendance-rule")
public class AttendanceRuleController {

    @DubboReference
    private IAttendanceRuleService attendanceRuleService;


    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody AttendanceRuleDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.save(dto));
    }

    @GetMapping("{ruleCode}")
    public ApiResponse<AttendanceRuleVO> getByRuleCode(@PathVariable String ruleCode){
        return ApiResponse.ok("SUCCESS",attendanceRuleService.getByRuleCode(ruleCode));
    }
    @GetMapping("/page")
    public ApiResponse<Page<AttendanceRuleVO>> page(@RequestBody AttendanceRuleQueryDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.page(dto));
    }


    @PutMapping("/{id}")
    public ApiResponse<Object> update(@PathVariable Long id, @RequestBody AttendanceRuleDTO dto) {
        dto.setId(id); // 确保ID传入
        attendanceRuleService.update(dto);
        return ApiResponse.ok();
    }


    @GetMapping("/custom/page")
    public ApiResponse<Page<AttendanceRuleVO>> customPage(AttendanceRuleQueryDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.customPage(dto));
    }


    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "规则ID不能为空");
        if (id < 0) {
            throw new RuntimeException("规则ID不能小于0");
        }
        attendanceRuleService.deleteById(id);
        return ApiResponse.ok();
    }
}
